var logisticsCompanyCode = '',
	//code = getValueByParam('code'), //企业微信、微信APP登录返回的code
	code = '3dfsdf3354dfgdgd45fgdg3455',
	config = {
		appId: 'wx916d70ac30e6eae1',
		timestamp: '145255565456',
		nonceStr: '20240129154400',
		signature: 'dff2f569339b63fe545c510f18feb2fd'
	},
	platform = '';

$(() => {
	initListener();
	platform = envjudge();
	if (platform == 'wx-mobile' || platform == 'com-wx-mobile') {
		queryScanConfig();
	}
})

initListener = () => {
	$(".textarea").bind('input propertychange', (event) => {
		let value = event.target.value;
		$(`.input_tip_box`).html(value.length + '/' + event.target.maxLength);
	});
	$("#pick_logistics").on('click', () => {
		selectLogistics('bill_logistics', res => {
			logisticsCompanyCode = res.code;
		});
	});
	$("#save").on('click', () => {
		onSubmit();
	});
	$("#scan").on('click', () => {
		if (platform == 'wx-mobile' || platform == 'com-wx-mobile') {
			onScan();
		} else {
			showToast('请使用微信或者企业微信客户端体验该功能！')
		}
	});
}

onSubmit = () => {
	let bill_no = $(`#bill_no`).val();
	if (bill_no == '') {
		showToast('请填写运单号');
		$(`#bill_no`).focus();
		return
	}
	let bill_logistics = $('#bill_logistics').html();
	if (bill_logistics == '') {
		showToast('请选择物流公司');
		return
	}
	let textarea = $(`.textarea`).val();
	let subLoading = layer.open({
		type: 2,
		shadeClose: false,
		content: '正在提交...'
	});
	setTimeout(() => {
		layer.close(subLoading);
		showToast('操作成功！');
		goBack();
	}, 500)
}

envjudge = () => {
	var isMobile = window.navigator.userAgent.match(
		/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
	) // 是否手机端
	var isWx = /micromessenger/i.test(navigator.userAgent) // 是否微信
	var isComWx = /wxwork/i.test(navigator.userAgent) // 是否企业微信
	if (isComWx && isMobile) {
		//手机端企业微信
		return 'com-wx-mobile'
	} else if (isComWx && !isMobile) {
		//PC端企业微信
		return 'com-wx-pc'
	} else if (isWx && isMobile) {
		// 手机端微信
		return 'wx-mobile'
	} else if (isWx && !isMobile) {
		// PC端微信
		return 'wx-pc'
	} else {
		return 'other'
	}
}

queryScanConfig = () => {
	code = '0e3j8A0w3D8bP13OdP2w3ZhDLL0j8A0s';
	var subLoading = layer.open({
		type: 2,
		shadeClose: false,
		content: '正在初始化...'
	});
	if (isEmpty(code)) {
		layer.close(subLoading);
		showToast('获取code参数失败');
		return
	}
	let postData = {
		code: code,
		url: encodeURI(window.location.href.split('#')[0])
	};
	let res = {
		code: 200,
		data: {
			appId: 'wx916d70ac30e6eae1',
			timestamp: '145255565456',
			nonceStr: '20240129154400',
			signature: 'dff2f569339b63fe545c510f18feb2fd',
		}
	}
	setTimeout(() => {
		config = res.data;
		layer.close(subLoading);
		onWechatConfig();
	}, 1000)
}

onWechatConfig = () => {
	showToast('正在配置...');
	wx.config({
		debug: false,
		appId: config.appId, //公众号  企业微信appid
		timestamp: config.timestamp, //时间戳
		nonceStr: config.nonceStr, //随机字符串
		signature: config.signature, //签名
		jsApiList: ['scanQRCode']
	});
	wx.ready(function() {
		wx.checkJsApi({ //判断当前客户端版本是否支持指定JS接口
			jsApiList: ['scanQRCode'],
			success: function(res) {
				if (res.checkResult.scanQRCode == true) {
					onScan();
				} else {
					showMessage('微信版本信息:<br>' + "抱歉，当前微信客户端版本不支持扫一扫。", 'error');
					//alert('抱歉，当前微信客户端版本不支持扫一扫')
				}
			},
			fail: function(res) { //检测getNetworkType该功能失败时处理
				//alert('checkJsApi error');
				showMessage('检查扫一扫权限:<br>' + "检查权限出错。", 'error');
			}
		});
	});
	wx.error(function(res) {
		showMessage("配置时错误信息:<br>" + JSON.stringify(res), 'error');
	});
}

onScan = () => {
	wx.scanQRCode({
		desc: 'scanQRCode desc',
		needResult: 1, // 默认为0，扫描结果由企业微信处理，1则直接返回扫描结果，
		scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
		success: function(res) {
			var result = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
			var resultObj = JSON.parse(result);
			showMessage('扫码结果信息:<br>' + resultObj, 'success');
		},
		error: function(res) {
			console.log(res)
			alert(JSON.stringify(res))
			if (res.errMsg.indexOf('function_not_exist') > 0) {
				//alert('微信版本过低请升级');
				showMessage('微信版本信息:<br>' + "微信版本过低请升级。", 'error');
			}
		}
	}); //wx.scanQRcode结束
}

showMessage = (msg,type) => {
	
}
